package com.ddx.controller.core;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.dto.ShortVideoDto;
import com.ddx.dto.UserDto;
import com.ddx.service.IUnconcernedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: 未关注个人中心
 * @date 2018/11/11
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/app")
public class UnconcernedController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    IUnconcernedService iUnconcernedService;

    @RequestMapping(value = "/user/userCenter", method = RequestMethod.POST)
    public ResultModel userCenter(@RequestBody UserDto userDto, HttpServletRequest request){
        String userId=request.getParameter("userId");
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            UserDto dto=iUnconcernedService.userCenter(userId);
            resultModel.setData(dto);
        }catch (Exception e){
            logger.error("未关注个人中心查询异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @RequestMapping(value = "/user/uploadVideo", method = RequestMethod.POST)
    public ResultModel uploadVideo(@RequestBody ShortVideoDto shortVideoDto, HttpServletRequest request){
        String userId=request.getHeader("userId");
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            iUnconcernedService.uploadVideo(shortVideoDto,userId);
        }catch (Exception e){
            logger.error("上传视频接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @RequestMapping(value = "/user/incomeFlow", method = RequestMethod.POST)
    public ResultModel incomeFlow(HttpServletRequest request){
        String userId=request.getHeader("userId");
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            Map<String,Object> map= iUnconcernedService.incomeFlow(userId);
            resultModel.setData(map);
        }catch (Exception e){
            logger.error("我的收入接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    @RequestMapping(value = "/user/coverPrivate", method = RequestMethod.POST)
    public ResultModel coverPrivate(@RequestBody ShortVideoDto shortVideoDto,HttpServletRequest request){
        String userId=request.getHeader("userId");
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            shortVideoDto.setUserId(Integer.parseInt(userId));
            iUnconcernedService.coverPrivate(shortVideoDto);
        }catch (Exception e){
            logger.error("转为私密作品接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @RequestMapping(value = "/user/deleteVideo", method = RequestMethod.POST)
    public ResultModel deleteVideo(@RequestBody ShortVideoDto shortVideoDto,HttpServletRequest request){
        String userId=request.getHeader("userId");
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            shortVideoDto.setUserId(Integer.parseInt(userId));
            iUnconcernedService.deleteVideo(shortVideoDto);
        }catch (Exception e){
            logger.error("删除视频接口异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
